package com.example.callmanagerandroid;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.database.Contact;
import com.example.user.User;

public class CallListActivity extends Activity implements OnItemClickListener,
		OnClickListener {

	private List<String> inComingCalls = new ArrayList<String>();
	private List<User> userList = new ArrayList<User>();
	private ListView list;
	private Button data;
	TextView tv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		setContentView(R.layout.call_list);

		Intent intent = new Intent(this, Myservice.class);
		startService(intent);

		LinearLayout bottom = (LinearLayout) findViewById(R.id.layout_bottom);
		bottom.setVisibility(View.GONE);

		list = (ListView) findViewById(R.id.listView1);
		list.setOnItemClickListener(this);
		data = (Button) findViewById(R.id.button1);
		tv = (TextView) findViewById(R.id.textView2);
		data.setOnClickListener(this);
		Tong();
		setUpUser(setUpInComingCall());
		list.setAdapter(new CallListActivityAdapter(userList, inComingCalls,
				CallListActivity.this));
	}

	private void Tong() {
		Contact contact = new Contact(CallListActivity.this);
		Cursor c = contact.selectCount();
		int count = 0;
		if (c != null) {
			if (c.moveToFirst()) {
				count = c.getInt(0);
			}
		}
		tv.setText(String.valueOf(count));
		c.close();
		contact.close();

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		ShowPopUp(userList.get(arg2), inComingCalls.get(arg2));
	}

	@Override
	public void onClick(View v) {
		if (v == data) {
			Intent intent = new Intent(CallListActivity.this,
					MainActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(intent);
			finish();
		}

	}

	private Boolean isExited(List<String> list, String name) {
		Boolean isEx = false;
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).equals(name)) {
				isEx = true;
				return isEx;
			}
		}
		return isEx;
	}

	private List<String> setUpInComingCall() {
		List<String> li = new ArrayList<String>();
		Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null,
				null, null, null);
		if (managedCursor != null)
			Log.w("", "c k sn");
		int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
		int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
		if (managedCursor.moveToLast()) {
			do {
				String callType = managedCursor.getString(type);
				int dircode = Integer.parseInt(callType);
				if (dircode == 1 || dircode == 3) {
					String phNumber = managedCursor.getString(number);
					if (li.size() < 10) {
						if (!isExited(li, phNumber))
							li.add(phNumber);
					} else {
						inComingCalls = li;
						return li;
					}
				}
			} while (managedCursor.moveToPrevious());
		}
		inComingCalls = li;
		return li;
	}

	private List<User> setUpUser(List<String> li) {
		List<User> userLi = new ArrayList<User>();
		Contact contact = new Contact(getApplicationContext());
		for (int i = 0; i < li.size(); i++) {
			Cursor c = contact.SelectName(li.get(i));
			User us = new User();
			if (c != null)
				if (c.moveToFirst()) {
					us.setKHID(c.getString(c.getColumnIndexOrThrow("KhID")));
					us.setMA(c.getString(c.getColumnIndexOrThrow("Ma")));
					us.setTEN(c.getString(c.getColumnIndexOrThrow("Ten")));
					us.setDIACHI(c.getString(c.getColumnIndexOrThrow("DiaChi")));
					us.setDIENTHOAI(c.getString(c
							.getColumnIndexOrThrow("DienThoai")));
					us.setTRANGTHAI(c.getString(c
							.getColumnIndexOrThrow("TrangThai")));
				}
			userLi.add(us);
			c.close();
		}
		userList = userLi;
		contact.close();
		return userLi;
	}

	private void ShowPopUp(User user, String munber) {

		Intent intent = new Intent(CallListActivity.this, phoneRing.class);
		intent.putExtra("number", munber);
		intent.putExtra("id", user.getMA());
		intent.putExtra("ten", user.getTEN());
		intent.putExtra("dt", user.getDIENTHOAI());
		intent.putExtra("dc", user.getDIACHI());
		intent.putExtra("gc", user.getTRANGTHAI());
		intent.putExtra("flag", 0);
		startActivity(intent);

		// String id = user.getMA();
		// String ten = user.getTEN();
		// String dt = user.getDIENTHOAI();
		// String dc = user.getDIACHI();
		// String gc = user.getTRANGTHAI();
		//
		// final Dialog dialog = new Dialog(CallListActivity.this);
		// dialog.setContentView(R.layout.custom_alert);
		// dialog.setTitle("Cuộc gọi đến");
		//
		// TextView number = (TextView) dialog.findViewById(R.id.tvphone);
		// number.setText(munber);
		// TextView tid = (TextView) dialog.findViewById(R.id.tvID);
		// tid.setText(id);
		// TextView ttk = (TextView) dialog.findViewById(R.id.tvName);
		// ttk.setText(ten);
		// TextView tdc = (TextView) dialog.findViewById(R.id.tvAddr);
		// tdc.setText(dc);
		// TextView tgc = (TextView) dialog.findViewById(R.id.tvGhiChu);
		// tgc.setText(gc);
		//
		// Button dialogButton = (Button) dialog.findViewById(R.id.button1);
		// dialogButton.setOnClickListener(new OnClickListener() {
		// @Override
		// public void onClick(View v) {
		// dialog.dismiss();
		// }
		// });
		//
		// dialog.show();
	}

}
